package com.alex.wechat.component.account.service.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alex.wechat.component.account.entity.WxComponentAccountEntity;
import com.alex.wechat.component.account.service.WxComponentAccountServiceI;
import com.gomore.jeecg.common.Assert;

@Service("wxComponentAccountService")
@Transactional
public class WxComponentAccountServiceImpl extends CommonServiceImpl
    implements WxComponentAccountServiceI {

  public void delete(WxComponentAccountEntity entity) throws Exception {
    super.delete(entity);
    // 执行删除操作增强业务
    this.doDelBus(entity);
  }

  public Serializable save(WxComponentAccountEntity entity) throws Exception {
    Serializable t = super.save(entity);
    // 执行新增操作增强业务
    this.doAddBus(entity);
    return t;
  }

  public void saveOrUpdate(WxComponentAccountEntity entity) throws Exception {
    super.saveOrUpdate(entity);
    // 执行更新操作增强业务
    this.doUpdateBus(entity);
  }

  @Override
  public WxComponentAccountEntity getAccountByAppId(String appId) {
    Assert.assertArgumentNotEmpty(appId, "appid");

    List<WxComponentAccountEntity> results = this.findByProperty(WxComponentAccountEntity.class,
        "appId", appId);
    if (results != null && results.size() > 0) {
      return results.get(0);
    } else {
      return null;
    }
  }

  @Override
  public WxComponentAccountEntity getAccountByLogin() {
    List<WxComponentAccountEntity> list = findByProperty(WxComponentAccountEntity.class,
        "isCurrentAccount", Boolean.TRUE);
    if (list.size() > 0) {
      return list.get(0);
    }

    list = loadAll(WxComponentAccountEntity.class);
    if (list.size() == 1) {
      return list.get(0);
    }

    return null;
  }

  /**
   * 新增操作增强业务
   * 
   * @param t
   * @return
   */
  private void doAddBus(WxComponentAccountEntity t) throws Exception {
  }

  /**
   * 更新操作增强业务
   * 
   * @param t
   * @return
   */
  private void doUpdateBus(WxComponentAccountEntity t) throws Exception {
  }

  /**
   * 删除操作增强业务
   * 
   * @param id
   * @return
   */
  private void doDelBus(WxComponentAccountEntity t) throws Exception {
  }

  private Map<String, Object> populationMap(WxComponentAccountEntity t) {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", t.getId());
    map.put("create_name", t.getCreateName());
    map.put("create_by", t.getCreateBy());
    map.put("create_date", t.getCreateDate());
    map.put("update_name", t.getUpdateName());
    map.put("update_by", t.getUpdateBy());
    map.put("update_date", t.getUpdateDate());
    map.put("sys_org_code", t.getSysOrgCode());
    map.put("sys_company_code", t.getSysCompanyCode());
    map.put("app_id", t.getAppId());
    map.put("app_secret", t.getAppSecret());
    map.put("name", t.getName());
    map.put("verify_ticket", t.getVerifyTicket());
    map.put("access_token", t.getAccessToken());
    map.put("access_token_expires_in", t.getAccessTokenExpiresIn());
    map.put("pre_auth_code ", t.getPreAuthCode());
    map.put("pre_auth_code_expires_in", t.getPreAuthCodeExpiresIn());
    map.put("domain", t.getDomain());
    map.put("token", t.getToken());
    map.put("aes_key", t.getAesKey());
    return map;
  }

  /**
   * 替换sql中的变量
   * 
   * @param sql
   * @param t
   * @return
   */
  public String replaceVal(String sql, WxComponentAccountEntity t) {
    sql = sql.replace("#{id}", String.valueOf(t.getId()));
    sql = sql.replace("#{create_name}", String.valueOf(t.getCreateName()));
    sql = sql.replace("#{create_by}", String.valueOf(t.getCreateBy()));
    sql = sql.replace("#{create_date}", String.valueOf(t.getCreateDate()));
    sql = sql.replace("#{update_name}", String.valueOf(t.getUpdateName()));
    sql = sql.replace("#{update_by}", String.valueOf(t.getUpdateBy()));
    sql = sql.replace("#{update_date}", String.valueOf(t.getUpdateDate()));
    sql = sql.replace("#{sys_org_code}", String.valueOf(t.getSysOrgCode()));
    sql = sql.replace("#{sys_company_code}", String.valueOf(t.getSysCompanyCode()));
    sql = sql.replace("#{app_id}", String.valueOf(t.getAppId()));
    sql = sql.replace("#{app_secret}", String.valueOf(t.getAppSecret()));
    sql = sql.replace("#{name}", String.valueOf(t.getName()));
    sql = sql.replace("#{verify_ticket}", String.valueOf(t.getVerifyTicket()));
    sql = sql.replace("#{access_token}", String.valueOf(t.getAccessToken()));
    sql = sql.replace("#{access_token_expires_in}", String.valueOf(t.getAccessTokenExpiresIn()));
    sql = sql.replace("#{pre_auth_code }", String.valueOf(t.getPreAuthCode()));
    sql = sql.replace("#{pre_auth_code_expires_in}", String.valueOf(t.getPreAuthCodeExpiresIn()));
    sql = sql.replace("#{domain}", String.valueOf(t.getDomain()));
    sql = sql.replace("#{token}", String.valueOf(t.getToken()));
    sql = sql.replace("#{aes_key}", String.valueOf(t.getAesKey()));
    sql = sql.replace("#{UUID}", UUID.randomUUID().toString());
    return sql;
  }

  /**
   * 执行JAVA增强
   */
  private void executeJavaExtend(String cgJavaType, String cgJavaValue, Map<String, Object> data)
      throws Exception {
    if (StringUtil.isNotEmpty(cgJavaValue)) {
      Object obj = null;
      try {
        if ("class".equals(cgJavaType)) {
          // 因新增时已经校验了实例化是否可以成功，所以这块就不需要再做一次判断
          obj = MyClassLoader.getClassByScn(cgJavaValue).newInstance();
        } else if ("spring".equals(cgJavaType)) {
          obj = ApplicationContextUtil.getContext().getBean(cgJavaValue);
        }
        if (obj instanceof CgformEnhanceJavaInter) {
          CgformEnhanceJavaInter javaInter = (CgformEnhanceJavaInter) obj;
          javaInter.execute(data);
        }
      } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("执行JAVA增强出现异常！");
      }
    }
  }
}